<template>
  <u-empty mode="history" v-if="result.data.length <= 0 && !result.loading"></u-empty>
  <page-loading :show="showPage" v-else>
    <view class="my-browsing-history">
      <uni-swipe-action>
        <block v-for="(item, index) in result.data" :key="index">
          <new-car-item @onClickAction="onClickAction" :item="item" :item-index="index" />
        </block>
      </uni-swipe-action>
      
      <view class="loading-status"><u-loadmore :status="result.status" /></view>
      <!-- 分享组件 -->
      <share v-model="shareShow" :is-download="false" @change="onChangeShare" />
    </view>
  </page-loading>
</template>

<script>
import uniSwipeAction from '@/components/uni-swipe/uni-swipe-action/uni-swipe-action.vue'
import newCarItem from './components/newCarItem.vue' // 新车样式组件
import {share} from '@/utils/common-api.js' // 导入分享方法
export default {
  components: {
    uniSwipeAction,
    newCarItem
  },
  data() {
    return {
      shareShow: false,
      result: {
        PageIndex: 1,
        PageSize: 20,
        isEnd: false,
        loading: false,
        status: 'loading',
        data: []
      },
      shareInfo: {},
      showPage: false,
    }
  },
  onLoad() {
    this.getList()
  },
  methods: {
    /*
      点击列表菜单
      @param {Object} data.type 操作类型 (share，delete)
      @param {Object} data.item 当前选择项的数据
      @param {Object} data.index 当前选择项在数组中的索引
      params: 
    */
    onClickAction(data) {
      if(data.type === 'share') {
        this.shareShow = true
        this.shareInfo = data
      } else {
        this.deleteCurrentItem(data)
      }
    },
    // 点击分享
    onChangeShare(data) {
      let params = {}
      let current = this.shareInfo.item
      params.title = current.configuration_name
      params.summary = current.configuration_name
      params.imageUrl = current.goodimg
      if(current.is_loans === 1) {
        params.href = this.$config.WEBURL + `index.html#/pages_commodity/giantnengjiangDetails/giantnengjiangDetails?goodid=${current.giftid}`
      } else {
        params.href = this.$config.WEBURL + `index.html#/pages_commodity/sourceDetails/sourceDetails?goodid=${current.giftid}`
      }
      share({
        scene: data,
        type: 0,
        ...params
      }, () => {
        
      })
    },
    // 删除选择项
    async deleteCurrentItem(data) {
      uni.showLoading({
        title: '加载中'
      })
      try{
        let res = await this.$api.user.DeleteInvoiceTitle({
          id: data.item.id
        })
        uni.hideLoading()
        if(res.code === 200) {
          this.result.data.splice(data.index, 1)
        }
        uni.showToast({
          icon: 'none',
          title: res.msg
        })
      }catch(e){
        uni.hideLoading()
        uni.showToast({
          icon: 'none',
          title: '删除记录失败'
        })
      }
    },
    // 计算总共有多少页
    pageCount (totalnum,limit){
      return totalnum > 0 ? ((totalnum < limit) ? 1 : ((totalnum % limit) ? (parseInt(totalnum / limit) + 1) : (totalnum / limit))) : 0
    },
    // 获取浏览记录
    async getList(type) {
      let res = this.result
      if (res.loading || res.isEnd) {
        return false;
      }
      res.loading = true;
      this.res = res
      
      try{
        let result = await this.$api.user.GetVisitList({
          pageIndex: res.PageIndex,
          pageSize: res.PageSize,
          type: 0,
          app_type: 1
        })
        let visitlist = result.data.list
        if (type) {
          res.data = []
        }
        // 如果返回数据列表为空，或者当前页数大于总页数代表已经到最后一页了
        if(visitlist.length <= 0 || res.PageIndex >= this.pageCount(result.data.total, res.PageSize)) {
          res.isEnd = true
          res.status = 'nomore'
        }
        
        if (visitlist.length > 0) {
          res.data = res.data.concat(visitlist)
          res.PageIndex = res.PageIndex + 1
        }
        res.loading = false
        this.res = res
        this.showPage = true
      }catch(e){
        //TODO handle the exception
      }
    }
  },
  // 页面滚动到底部加载更多
  onReachBottom() {
    this.getList()
  }
}
</script>

<style lang="scss">
page{
  height: 100%;
}
/deep/ .item-swipe:last-child .new-car-item{
  border-bottom: none;
}
.loading-status{
  padding: 40rpx 0;
}
</style>
